package com.sduept.nwld.dataserver.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.fault.manager.FaultRecordQueryManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.manager.countanalyse.ProtectAnalysisManger;
import com.sduept.nwld.dataserver.manager.statistics.StatisticsCommonManager;
import com.sduept.nwld.dataserver.manager.statistics.TransmissionEquipmentManager;
import com.sduept.nwld.dataserver.model.countanalyse.BreakerException;
import com.sduept.nwld.dataserver.model.countanalyse.BusbarInfo;
import com.sduept.nwld.dataserver.model.countanalyse.TansformerInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 *厂站输电设备故障统计
 */
@RestController
@RequestMapping("/transmission")
public class TransmissionEquipmentService {
	@Autowired
	private SuperDAO dao;
	@Autowired
	private ProtectAnalysisManger pa;
	@Autowired
	private StatisticsCommonManager comM;
	@Autowired
	private TransmissionEquipmentManager manager;
	@Autowired
	private FaultRecordQueryManager frqm;
	
	/**
	 * 获取开始年到结束年之间的变压器的台数，新增台数，故障次数
	 * @return[{'name':'2012','value':[{'name':'变压器台数','value':'381'},{'name':'新增台数','value':'37'},{'name':'故障次数','value':'15'}]}]
	 */

	@GetMapping("/getTransmissionFaultCountData/{startYear}/{endYear}/{voltage}")
	public String getTransmissionFaultCount(@PathVariable("startYear") String startYear,@PathVariable("endYear") String endYear,@PathVariable("voltage") String voltage) {
		List<Object> jsonList = new ArrayList<Object>();
		Integer vol = null;
		if(!"0".equals(voltage)) {
			vol = Integer.parseInt(voltage);
		}
		List<FaultRecord> faultRecords = comM.getFaultRecordsByCondition(startYear, endYear, voltage, "变压器");
		Map<Integer, List<FaultRecord>> yearMap = comM.genFaultRecordMapByYear(faultRecords);
		comM.completeMapByYear(startYear, endYear, yearMap);
		for (Integer year : yearMap.keySet()) {
			List<FaultRecord> faults = yearMap.get(year);
			Map<String, Object> map = new HashMap<String, Object>();
			List<Object> list = new ArrayList<Object>();
			Map<String, String> allMap = new HashMap<String, String>();
			Map<String, String> newMap = new HashMap<String, String>();
			Map<String, String> brokenMap = new HashMap<String, String>();
			List<TansformerInfo> tansformerInfos = manager.getTansformerInfos(year, vol);
			TansformerInfo info = new TansformerInfo();
			if(tansformerInfos.size()>0) {
				info = tansformerInfos.get(0);
			} else {
				info.setCount(0);
				info.setNewAdd(0);
			}
			allMap.put("name", "变压器台数");
			allMap.put("value", info.getCount() + "");
			brokenMap.put("name", "故障次数");
			brokenMap.put("value", faults.size()+"");
			newMap.put("name", "新增台数");
			newMap.put("value", info.getNewAdd()+"");

			list.add(allMap);
			list.add(brokenMap);
			list.add(newMap);
			map.put("name", year);
			map.put("value", list);
			jsonList.add(map);
		}
		Collections.reverse(jsonList);
		String str = JSONObject.toJSONString(jsonList);
		return str;
	}

	/**
	 * 根据年、电压等级统计变压器故障
	 * @param year
	 * @param voltage
	 * @return
	 */

	@GetMapping("/getTransFaultByYearAndVoltage/{year}/{voltage}")
	public String getTransFaultByYearAndVoltage(@PathVariable("year") String year,@PathVariable("voltage") String voltage) {
		// 根据年、电压等级获取类型为线路的故障
		List<FaultRecord> faultRecords = comM.getFaultRecordsByCondition(year, year, voltage, "变压器");
		return comM.genJSONData(faultRecords);
	}
	
	/**
	 * 获取历年母线的长度，新增长度及故障次数
	 * @return[{'name':'2012','value':[{'name':'变压器台数','value':'381'},{'name':'新增台数','value':'37'},{'name':'故障次数','value':'15'}]}]
	 */

	@GetMapping("/getBusbarFaultCountData/{startYear}/{endYear}/{voltage}")
	public String getBusbarFaultCountData(@PathVariable("startYear") String startYear,@PathVariable("endYear") String endYear,@PathVariable("voltage") String voltage) {
		List<Object> jsonList = new ArrayList<Object>();
		Integer vol = null;
		if(!"0".equals(voltage)) {
			vol = Integer.parseInt(voltage);
		}
		List<FaultRecord> faultRecords = comM.getFaultRecordsByCondition(startYear, endYear, voltage, "母线");
		Map<Integer, List<FaultRecord>> yearMap = comM.genFaultRecordMapByYear(faultRecords);
		comM.completeMapByYear(startYear, endYear, yearMap);
		for (Integer year : yearMap.keySet()) {
			List<FaultRecord> faults = yearMap.get(year);
			Map<String, Object> map = new HashMap<String, Object>();
			List<Object> list = new ArrayList<Object>();
			Map<String, String> allMap = new HashMap<String, String>();
			Map<String, String> newMap = new HashMap<String, String>();
			Map<String, String> brokenMap = new HashMap<String, String>();
			List<BusbarInfo> busbarInfos = manager.getBusbarInfos(year, vol);
			BusbarInfo info = new BusbarInfo();
			if(busbarInfos.size()>0) {
				info = busbarInfos.get(0);
			} else {
				info.setCount(0);
				info.setNewAdd(0);
			}
			allMap.put("name", "母线条数");
			allMap.put("value", info.getCount() + "");
			brokenMap.put("name", "故障次数");
			brokenMap.put("value", faults.size()+"");
			newMap.put("name", "新增条数");
			newMap.put("value", info.getNewAdd()+"");

			list.add(allMap);
			list.add(brokenMap);
			list.add(newMap);
			map.put("name", year);
			map.put("value", list);
			jsonList.add(map);
		}
		Collections.reverse(jsonList);
		String str = JSONObject.toJSONString(jsonList);
		return str;
	}


	@GetMapping("/getBusbarFaultByYearAndVoltage/{year}/{voltage}")
	public String getBusbarFaultByYearAndVoltage(@PathVariable("year") String year,@PathVariable("voltage") String voltage) {
		// 根据年、电压等级获取类型为线路的故障
		List<FaultRecord> faultRecords = comM.getFaultRecordsByCondition(year, year, voltage, "母线");
		return comM.genJSONData(faultRecords);
	}
	
	/**
	 * 开关设备异常
	 * */

	@GetMapping("/getBreakerExceptionByYear/{year}")
	public String getBreakerExceptionByYear(@PathVariable("year") String year) {
		List<BreakerException> breakers=pa.getBreakerExceptionByYear(year);
		return JSONArray.toJSONString(breakers);
	}


	@GetMapping("/getAllBreakerException")
	public String getAllBreakerException() {
		List<BreakerException> breakers=pa.getAllBreakerException();
		return JSONArray.toJSONString(breakers);
	}
	
}
